package cz.cvut.fit.mi_paa.bucket;

/**
 * Created with IntelliJ IDEA.
 * User: milos
 * Date: 19.10.12
 * Time: 2:22
 * To change this template use File | Settings | File Templates.
 */
public class Bucket {
    private int capacity;
    private int volume;

    public Bucket(int volume, int capacity) {
        this.volume = volume;
        this.capacity = capacity;
    }

    public int getCapacity() {
        return capacity;
    }

    public int getVolume() {
        return volume;
    }

    public int getFreeSpace() {
        return capacity - volume;
    }

    public boolean fill() {
        if(!isFull()) {
            volume=capacity;
            return true;
        }
        return false;
    }

    public boolean empty() {
        if(!isEmpty()) {
            volume=0;
            return true;
        }
        return false;
    }

    public void add(int amount) {
        volume+=amount;
    }

    public int pour(int amount) {
        volume-=amount;
        return amount;
    }

    public int pour() {
        return pour(volume);
    }

    public boolean hasEnoughWater(int amount) {
        return (amount<=volume);
    }

    public boolean equalsVolume(int amount) {
        return amount==volume;
    }

    public boolean isEmpty() {
        return equalsVolume(0);
    }

    public boolean isFull() {
        return equalsVolume(capacity);
    }

    @Override
    public String toString() {
        return "["+ capacity + "," + volume + "]";
    }

    @Override
    public Bucket clone() throws CloneNotSupportedException {
        return new Bucket(volume,capacity);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Bucket bucket = (Bucket) o;

        if (capacity != bucket.capacity) return false;
        if (volume != bucket.volume) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = capacity;
        result = 31 * result + volume;
        return result;
    }
}
